package com.atguigu.springcloudalibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloudalibaba.myHandler.ConsumerBlockHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RateLimitController {
    private static final Logger logger = LoggerFactory.getLogger(RestController.class);

    /**
     * 按资源名称限流测试
     * @return
     */
    @GetMapping(value = "/byResource")
    @SentinelResource(value = "byResource",blockHandler = "exception")
    public CommonResult byResource()
    {
        return new CommonResult(200,"按资源名称限流测试",new Payment(2021L,"serial001"));
    }
    //兜底方法
    public CommonResult exception(BlockException blockException){
        return new CommonResult(444,blockException.getClass().getCanonicalName()+"\t"+"服务降级");
    }

    /**
     * 按资url限流测试
     * @return
     */
    @GetMapping(value = "/rateLimit/byUrl")
    @SentinelResource(value = "byUrl")
    public CommonResult byUrl()
    {
        return new CommonResult(200,"按资url限流测试",new Payment(2021L,"serial002"));
    }

    /**
     * 按客户自定义方法进行兜底方法的选择
     * @return
     */
    @GetMapping(value = "/rateLimit/consumerBlockHandler")
    @SentinelResource(value = "consumerBlockHandler",
            blockHandlerClass = ConsumerBlockHandler.class,
            blockHandler = "exception2")
    public CommonResult consumerBlockHandler()
    {
        return new CommonResult(200,"按客户自定义方法进行兜底方法的选择",new Payment(2021L,"serial003"));
    }
}
